﻿/// <reference path="../knockout-2.2.0.debug.js" />

//simple jQuery based viewmodel without Knokout  
; (function (root) {
    var app = root.app;     
    var toasts = [];
   

    var vm = {        
        $Ocasion: {},
        SOrigin: {},
        $slidenote: {},

        setToastsData: setToastsData,        
        loadStart: loadStart,
        loadStop: loadStop,
        bind: function ($occasion, $origin, $searchBtn, $slidenote) {
            this.$Occasion = $occasion;
            this.$Origin = $origin;
            this.$slidenote = $slidenote;
            $searchBtn.on('click', search);

        }

    };


    app.indexViewModel = vm;

    function loadStart(contentId, loaderId) {
        $("#" + contentId).fadeOut(100);
        $("#" + loaderId).fadeIn(100);
    }

    function loadStop(contentId, loaderId) {
        $("#" + loaderId).hide(100);
        $("#" + contentId).fadeIn();


    }

    function setToastsData(data) {
        toasts = data;
        loadSlideNote(data)
        
    }

    //here is how to load data to SlideNote:
    //$('#slidenoteid').data('SlideNote').loadData(data);

    function loadSlideNote(data) {
        vm.$slidenote.data('SlideNote').loadData(data);
    }

    function search() {
       // root.console.log(vm.$Occasion.val())
        var toasts = selectToasts(vm.$Occasion.val(), vm.$Origin.val());
         
            //reinitialize plugin data
            loadSlideNote(toasts)
        
    }

    function selectToasts(occasion, origin) {
        if (occasion === "Select All" && origin === "Select All")
            return selectAll();
        if (occasion === "Select All")  
            return selectByOrigin(origin);
        else if(origin==="Select All") 
            return selectByOccasion(occasion);
        else
            return selectByOccasionAndOrigin(occasion, origin);
 
    }

    function selectAll( ) {
        var results = [];

        for (var i = 0, length = toasts.length; i < length; i++) {
             
                results.push(toasts[i]);
             
        }
        return results;
    }

    function selectByOrigin(origin) {
        var results = [];

        for (var i = 0, length = toasts.length; i < length; i++) {
            var item = toasts[i];
            if (item.Origin === origin) {
                results.push(item);
            }
        }
        return results;
    }

    function selectByOccasion(occasion) {
        var results = [];

        for (var i = 0, length = toasts.length; i < length; i++) {
            var item = toasts[i];
            if (item.Occasion === occasion) {
                results.push(item);
            }
        }
        return results;

    }

    function selectByOccasionAndOrigin(occasion, origin) {
        var results = [];

        for (var i = 0, length = toasts.length; i < length; i++) {
            var item = toasts[i];
            if (item.Occasion === occasion && item.Origin === origin) {
                results.push(item);
            }
        }
        return results;
    }



       

     

}(window));